﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace FlowerPot
{
    public class RestClient
    {
        public RestClient()
        {
        }

        

        #region Delete方式
        public string Delete(string data, string uri)
        {
            return CommonHttpRequest(data, uri, "DELETE");
        }

        public string Delete(string uri)
        {
            //Web访问对象64
            string serviceUrl = null;

            serviceUrl = uri;

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            myRequest.Method = "DELETE";
            // 获得接口返回值68
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd());
            string ReturnXml = reader.ReadToEnd();
            reader.Close();
            myResponse.Close();
            return ReturnXml;
        }
        #endregion

        #region Put方式
        public string Put(string data, string uri)
        {
            return CommonHttpRequest(data, uri, "PUT");
        }
        #endregion

        #region POST方式实现

        public string Post(string data, string uri)
        {
            return CommonHttpRequest(data, uri, "POST");
        }

        public string CommonHttpRequest(string data, string uri, string type)
        {
            //Web访问对象，构造请求的url地址
            string serviceUrl = null;

            serviceUrl = uri;

            //构造http请求的对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            //转成网络流
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
            //设置
            myRequest.Method = type;
            myRequest.ContentLength = buf.Length;
            myRequest.ContentType = "application/json";

            String ip = ExtractIpAddress(uri);
            if (ip != null && !ip.Trim().Equals(""))
            {
                myRequest.Host = ip;
                //Form1.showLogs("获取ip地址：" + ip);
            }

            //if (uri.IndexOf("61.171.55.248") >= 0)
            //{
            //    myRequest.Host = "61.171.55.248";
            //}
            //else if (uri.IndexOf("36.111.185.83") >= 0)
            //{
            //    myRequest.Host = "36.111.185.83";
            //}
            //else
            //{ 
            //}

            myRequest.Accept = "*/*";
            myRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            myRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

            
            // 发送请求
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(buf, 0, buf.Length);
            newStream.Close();
            // 获得接口返回值
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string ReturnXml = reader.ReadToEnd();
            reader.Close();
            myResponse.Close();
            return ReturnXml;
        }
        #endregion

        #region GET方式实现
        public string Get(string uri)
        {
            //Web访问对象64
            string serviceUrl = null;

            serviceUrl = uri;

            //构造一个Web请求的对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            // 获得接口返回值68
            //获取web请求的响应的内容
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            //通过响应流构造一个StreamReader
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd());
            string ReturnXml = reader.ReadToEnd();
            reader.Close();
            myResponse.Close();
            return ReturnXml;
        }
        #endregion

        public static string ExtractIpAddress(string url)
        {
            Uri uri = new Uri(url);
            string host = uri.Host;
            // 检查主机是否为 IP 地址
            if (IPAddress.TryParse(host, out IPAddress ipAddress))
            {
                return host;
            }
            // 如果主机不是 IP 地址，则尝试解析域名
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(host);
                if (hostEntry.AddressList.Length > 0)
                {
                    return hostEntry.AddressList[0].ToString();
                }
            }
            catch (Exception)
            {
                // 解析域名时发生异常，返回空字符串
                return string.Empty;
            }
            return string.Empty;
        }
    }
}


